package com.pengshun.modules.common;

import com.alibaba.fastjson.JSON;
import com.pengshun.modules.jwt.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author luoligang
 * @create 2021/4/8
 */
public class TokenUtil {

    public static Long getTokenUserId() {
        String authHeader = getRequest().getHeader("jwtToken");
        JwtTokenUtil util = new JwtTokenUtil();
        if (authHeader != null) {
            final String authToken = authHeader;
            Claims claims = util.parseToken(authToken);
            if (null == claims) {
                return  null;
            }
            Long uiId = Long.valueOf(claims.get("uiId").toString());
            return  uiId;
        }
        return  null;
    }

    /**
     * 获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }
}
